//https://leetcode-cn.com/problems/path-sum-iii/description/
struct TreeNode {
    int val;
    struct TreeNode *left;
    struct TreeNode *right;
};

void countPaths(struct TreeNode *root, int sum, int *count, int *preVal) {
    int sumC;
    int **pp = &preVal;
    if(root) {
        sumC = root->val;
        countPaths(root->left, sum, count, &sumC);
        countPaths(root->right, sum, count, &sumC);
        if(sumC == sum) (*count)++;
        while(*pp){
            sumC += **pp;
            if(sumC == sum) (*count)++;
            pp = (int**)((char*)pp + ((char*)preVal - (char*)&sumC));
        }
        
    }
}

int pathSum(struct TreeNode* root, int sum) {
    int count = 0;
    countPaths(root, sum, &count, (void*)0);
    return count;
}
